# coding=utf-8
import struct

import traceback

import D2Ext
from D2Ext import LogMsg as Log

from gui.CButton import CButton
from gui.CCheckBox import CCheckBox
from gui.CDialog import CDialog
from gui.CEditBox import CEditBox
from gui.CGuiApplication import CGuiApplication
from gui.CResource import CResource
from gui.CWidget import CWidget
from apps.client.skill.skill_page import SkillPage

from gui.CPanel import CPanel

# from windows import *
app = CGuiApplication()

# background = CPanel()


def test_result():
    return True


def on_click_widget(*args, **kwargs):
    D2Ext.View_PrintTextOnTop(u'你点击了widget！', 1)


def on_click_widget1(*args, **kwargs):
    import struct
    from api.gamei.CGame import CGame
    msg = struct.pack("=BHH", 0x0C, 0, 0)
    CGame.SendMsgToServer(5, 0, msg)
    D2Ext.View_PrintTextOnTop(u'你使用了技能：心灵传动！', 1)


def on_click_widget2(*args, **kwargs):
    D2Ext.View_PrintTextOnTop(u'你选中了勾选框！', 1)


def main():
    try:
        Log("main")
        from api.gamei.CClient import CClient
        from apps.misc_functions import AppConfig

        AppConfig().install()

        CResource.add_image("data\\SJ_PATH\\SPELL\\skill_ama_a")
        CResource.add_image("data\\SJ_PATH\\Metro\\BigButton")
        CResource.add_image("data\\SJ_PATH\\Metro\\checkbox")
        CResource.add_image("data\\SJ_PATH\\Metro\\InputBox")
        CResource.add_image("data\\SJ_PATH\\Metro\\checkbox")
        CResource.add_image("data\\SJ_PATH\\Metro\\InputDlg")

        SkillPage()

    except Exception as e:
        Log(traceback.format_exc())
    return 0


def draw():
    try:
        return CGuiApplication.draw()
    except Exception as e:
        D2Ext.LogMsg(traceback.format_exc())
        return False


def event(msg, param):
    try:
        ret = CGuiApplication.event(msg, param)
        return ret
    except Exception as e:
        D2Ext.LogMsg(traceback.format_exc())
        return False


def enter_game():
    try:
        D2Ext.View_PrintTextOnTop(u'欢迎使用Diablo Metro System！', 1)

        return

        test_widget = CWidget()
        test_widget.set_position(0, 100)
        test_widget.set_size(100, 100)
        test_widget.set_hover_text("This is a white widget!")
        test_widget.connect(CWidget.SIGNAL_ONCLICK, on_click_widget)
        test_widget.show()

        test_button = CButton()
        test_button.set_image("data\\SJ_PATH\\Metro\\BigButton")
        test_button.set_hover_text("This is a button!")
        test_button.connect(CWidget.SIGNAL_ONCLICK, on_click_widget1)
        test_button.set_position(0, 210)
        test_button.set_button_frame(0, 1)
        test_button.set_text(u"打开箱子")
        test_button.show()

        test_checkbox = CCheckBox()
        test_checkbox.set_image("data\\SJ_PATH\\Metro\\checkbox")
        test_checkbox.set_hover_text("This is a checkbox!")
        test_checkbox.connect(CWidget.SIGNAL_ONCLICK, on_click_widget2)
        test_checkbox.set_position(0, 260)
        test_checkbox.set_check_frame(0, 1)
        test_checkbox.set_text(u"启用双倍经验")
        test_checkbox.show()

        test_editbox = CEditBox()
        test_editbox.set_image("data\\SJ_PATH\\Metro\\InputBox")
        test_editbox.set_hover_text("This is a editbox!")
        test_editbox.set_position(0, 310)
        test_editbox.set_text_offset(0, -20)
        test_editbox.show()

        test_dialog = CDialog()
        test_dialog.set_image("data\\SJ_PATH\\Metro\\InputDlg")
        test_dialog.set_frame(0)
        D2Ext.LogMsg("test_dialog.image_file=%s" % test_dialog.image_file)
        test_dialog.set_title(u"提示信息")
        test_dialog.set_text(u"确定退出？")
        test_dialog.set_position(0, 360)
        test_dialog.show()
    except Exception as e:
        D2Ext.LogMsg(traceback.format_exc())
        return


def draw_skill_page():
    # D2Ext.View_PrintTextOnTop(u'draw_skill_page！', 1)

    try:
        sp = SkillPage()
        sp.show()
    except Exception as e:
        D2Ext.LogMsg(traceback.format_exc())
    return

